package user;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Formula;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import pari.Defi;
import pari.Pari;


@Entity
@Table(name="User")
public class User implements Serializable {
	/* ------------------------------------------------------------ ATTRIBUTS */
	@Id
	@Column(name="id_user")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id_user;
	
	@Column(name="mail", unique=true)
	private String mail;
	
	@Column(name="pseudo")
	private String pseudo;
	
	@Column(name="password")
	private String password;
	
	@Column(name="points")
	private int points;
	
	@Column(name="score")
	private int score;
	
	@Column(name="nbParisTotal")
	private int nbParisTotal;
	
	@Column(name="nbParisJustes")
	private int nbParisJustes;
	
	
	//@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@ManyToMany(cascade=CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	public List<Friendship> friendship;
	
	//@OneToMany(fetch=FetchType.LAZY) 
	@OneToMany
	@LazyCollection(LazyCollectionOption.FALSE)
	public List<Pari> listparis;
	
	//@ManyToMany(fetch=FetchType.LAZY)  
	@ManyToMany
	@LazyCollection(LazyCollectionOption.FALSE)
	public List<Defi> listdefi;
	
	@Formula(value = "nbParisJustes/nbParisTotal")
	public Integer ratio;


	/* --------------------------------------------------------- CONSTRUCTEUR */
	public User() {
		super();
	}
	
	public User(String mail, String pseudo, String password) {
		this.id_user = 1;
		this.mail = mail;
		this.pseudo = pseudo;
		this.password = password;
		this.points = 100;
		this.score = 0;
		this.nbParisTotal = 0;
		this.nbParisJustes = 0;
		this.friendship = new ArrayList<Friendship>();
		this.listparis = new ArrayList<Pari>();
		this.listdefi = new ArrayList<Defi>();
	}

	//TODO: a supprimer par antoine car utiliser par ses tests
	public User(int id, String mail, String pseudo, int points, int score, int nbParisTotal, int nbParisJustes) {
		this(mail, pseudo, "");
		this.id_user = id;
		this.mail = mail;
		this.pseudo = pseudo;
		this.points = points;
		this.score = score;
		this.nbParisTotal = nbParisTotal;
		this.nbParisJustes = nbParisJustes;
	}


	/* ----------------------------------------------------------- ACCESSEURS */
	public int getId_user() { return id_user; }

	public void setId_user(int id_user) { this.id_user = id_user; }

	public String getMail() { return mail; }

	public void setMail(String mail) { this.mail = mail; }

	public String getPseudo() { return pseudo; }

	public void setPseudo(String pseudo) { this.pseudo = pseudo; }

	public String getPassword() { return password; }

	public void setPassword(String password) { this.password = password; }

	public int getPoints() { return points; }

	public void setPoints(int points) { this.points = points; }

	public int getScore() { return score; }

	public void setScore(int score) { this.score = score; }

	public int getNbParisTotal() { return nbParisTotal; }

	public void setNbParisTotal(int nbParisTotal) { this.nbParisTotal = nbParisTotal; }

	public int getNbParisJustes() { return nbParisJustes; }

	public void setNbParisJustes(int nbParisJustes) { this.nbParisJustes = nbParisJustes; }


	public List<Friendship> getFriends() { return friendship; }

	public List<Defi> getDefis() { return listdefi; }
	
	public List<Pari> getParis() { return listparis; }
	
	public void addDefi(Defi defi) { this.listdefi.add(defi); };
	
	public void addPari(Pari pari) { this.listparis.add(pari); }
	
	public  void dellPari(Pari pari) { this.listparis.remove(pari); }
	
	public void addFriend(Friendship friends) { this.friendship.add(friends); }
	
	public void delFriend(Friendship friends) { this.friendship.remove(friends); }

	public Integer getRatio() {
		return ratio;
	}

	public void setRatio(Integer ratio) {
		this.ratio = ratio;
	}
	
	@Override
	public boolean equals(Object o){
		if(o instanceof User && ((User)o).getMail().equals(this.getMail())){
			return true;
		}
		return false;
	}
	
}
